using M13.Game.Item;
using JetBrains.Annotations;
using UnityEngine;
using M13.Game.Item.Weapon;

namespace M13.GameSystem.Inventory
{
    public struct InventoryCell
    {
        public int ItemId { get; private set; }
        public int DurabilityValue { get; private set; }
        public int Count { get; private set; }
        public bool Empty { get; private set; }

        public InventoryCell(bool empty)
        {
            Empty = true;
            ItemId = -1;
            DurabilityValue = -1;
            Count = -1;
        }
        public void SetCell(int id, int durabilityValue, int count)
        {
            if(id == -1)
            {
                EmptyCell();
            }
            else
            {
                ItemId = id;
                Empty = false;
                DurabilityValue = durabilityValue;
                Count = count;

            }
        }

        public void SetDurabilityValue(int value)
        {
            if (Items.GetItemIntance(ItemId) == null) return;
            DurabilityValue = Mathf.Clamp(value, -1, ((Weapon)Items.GetItemIntance(ItemId)).WeaponProperties.MaxDurabilityValue);
        }

        public void SetCount(int value)
        {
            if(Items.GetItemIntance(ItemId) == null) return;
            Count = Mathf.Clamp(value, 0, Items.GetItemIntance(ItemId).ItemProperties.MaxStackCount);
        }

        public void EmptyCell()
        {
            Empty = true;
            ItemId = -1;
            DurabilityValue = -1;
            Count = -1;
        }

        public bool IsEmptyCell()
        {
            return Empty;
        }
    }

    public class Inventory
    {
        public int CellCount { get; private set; }
        public InventoryCell[] Cells { get; private set; }

        public Inventory(int cellCount)
        {
            Cells = new InventoryCell[cellCount];
            CellCount = cellCount;
            for (int i = 0; i < cellCount; i++)
            {
                Cells[i] = new InventoryCell(true);
            }
        }

        public void InsertItemIntoInventory(int index, InventoryCell otherAccessInventory)
        {
            Cells[index] = otherAccessInventory;
        }

        [CanBeNull]
        public Item GetItem(int index)
        {
            return Items.GetItemIntance(Cells[index].ItemId);
        }

        /// <summary>
        /// 减少耐久
        /// </summary>
        /// <param name="index"></param>
        /// <returns>减少耐久后是否清零</returns>
        public bool ReduceDurability(int index)
        {
            Cells[index].SetDurabilityValue(Cells[index].DurabilityValue - 1);
            if(Cells[index].DurabilityValue == 0)
            {
                Cells[index].EmptyCell();
                return true;
            }
            return false;
        }

        public bool ReduceCount(int index)
        {
            Cells[index].SetCount(Cells[index].Count - 1);
            if (Cells[index].Count == 0)
            {
                Cells[index].EmptyCell();
                return true;
            }
            return false;
        }

        public bool ReduceCount(int index, int reduceCount)
        {
            Cells[index].SetCount(Cells[index].Count - reduceCount);
            if (Cells[index].Count == 0)
            {
                Cells[index].EmptyCell();
                return true;
            }
            return false;
        }

        public bool IsEmptyCell(int index)
        {
            return Cells[index].Empty;
        }

        

        private InventoryCell AccessInventory(int index)
        {
            return Cells[index];
        }
    }
}